package com.lab.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lab.domain.ResultWithData;
import com.lab.domain.User;
import com.lab.domain.Util;
import com.lab.service.UserService;
import com.lab.utils.HttpStatus;
import com.lab.utils.HttpUtil;
import com.lab.utils.JsonUtils;
import com.lab.utils.PageUtil;
import com.lab.utils.jsonDataDomain;

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserService userService;
	
	@RequestMapping("/login")
	public void login(@RequestBody Map<String,String> map,HttpServletRequest req,HttpServletResponse response) 
	{
		User user=userService.login(map.get("id"), map.get("password"));
		Map<String, Object> m=new HashMap<String, Object>();
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		ResultWithData<Map> jsonData=new ResultWithData<Map>();
		if(null==user) 
		{
			jsonData.setMsg("用户名或密码错误");
			jsonData.setStatus(HttpStatus.ERROR);
		}else 
		{
			jsonData.setMsg("登陆成功！");
			jsonData.setStatus(HttpStatus.OK);
			m.put("uuid", uuid);
			m.put("user",user);
			jsonData.setData(m);
			req.setAttribute(uuid, user);
		}
		HttpUtil.send(response, JsonUtils.ToJson(jsonData));
	}
	
	@RequestMapping("/add")
	public void add(@RequestBody Map<String,String> map,HttpServletRequest req,HttpServletResponse response) 
	{
//		User superUser=(User) req.getAttribute(map.get("mac"));
//		
//		if(null==superUser) 
//		{
//			return ;
//		}else if(superUser.getType()!=6)
//		{
//			return ;
//		}
		User user=new User();
		user.setAddress(map.get("address"));
		user.setBirth(map.get("birth"));
		user.setClass1(map.get("class1"));
		user.setEmail(map.get("email"));
//		user.setHeadImg(map.get("headImg"));
		user.setId(map.get("id"));
		user.setIdCard(map.get("idCard"));
		user.setName(map.get("name"));
		user.setOtherInfo(map.get("otherInfo"));
		user.setPassword("123456");
//		user.setPassword(map.get("password"));
		user.setPhoneNumber(map.get("phoneNumber"));
		user.setSex(map.get("sex"));
		user.setStatus(1);
		user.setTag(map.get("tag"));
		user.setType(Integer.valueOf(map.get("type")));
		int result=userService.add(user);
		Util u=new Util();
		if(result==1) 
		{
			u.setStatus(HttpStatus.OK);
			u.setMsg("添加成功!");
		}else 
		{
			u.setStatus(HttpStatus.ERROR);
			u.setMsg("添加失败！");
		}
		HttpUtil.send(response, JsonUtils.ToJson(u));
	}
	@RequestMapping("/find")
	public void find(HttpServletRequest request,HttpServletResponse response)
	{
		Map<String, String> map=new HashMap<String, String>();
		String name=request.getParameter("name");
		String sex=request.getParameter("sex");
		String type=request.getParameter("type");
		int rows=Integer.valueOf(request.getParameter("rows"));
		int page=Integer.valueOf(request.getParameter("page"));
		if(null!=name&&!"".equals(name)) {
			map.put("name", name);
		}
		if(null!=sex&&!"".equals(sex)) {
			map.put("sex", sex);
		}
		if(null!=type&&!"".equals(type)) {
			map.put("type", type);
		}
		List<User> list=userService.find(map);
//		Util<List> jsonData=new Util<List>();
//		jsonData.setData(list);
//		jsonData.setMsg("ok");
//		jsonData.setStatus(HttpStatus.OK);
//		jsonData.setUrl("/");
		jsonDataDomain<List> o=new jsonDataDomain<List>();
		if(null==list) 
		{
			o.setTotal(0);
		}else 
		{
			o.setTotal(list.size());
			list=PageUtil.dealData(list, page, rows);
		}
		o.setRows(list);
		HttpUtil.send(response, JsonUtils.DataToJson1(o));
	}
	
	@RequestMapping("/update")
	public void update(@RequestBody Map<String,String> map,HttpServletResponse response) 
	{
		User user=new User();
		user.setAddress(map.get("address"));
		user.setBirth(map.get("birth"));
		user.setClass1(map.get("class1"));
		user.setEmail(map.get("email"));
//		user.setHeadImg(map.get("headImg"));
		user.setId(map.get("id"));
		user.setIdCard(map.get("idCard"));
		user.setName(map.get("name"));
		user.setOtherInfo(map.get("otherInfo"));
		user.setPassword(map.get("password"));
		user.setPhoneNumber(map.get("phoneNumber"));
		user.setSex(map.get("sex"));
		user.setStatus(Integer.valueOf(map.get("status")));
		user.setTag(map.get("tag"));
		user.setType(Integer.valueOf(map.get("type")));
		int result=userService.update(user);
		Util u=new Util();
		if(result==1) 
		{
			u.setStatus(HttpStatus.OK);
			u.setMsg("更新成功!");
		}else 
		{
			u.setStatus(HttpStatus.ERROR);
			u.setMsg("更新失败！");
		}
		HttpUtil.send(response, JsonUtils.ToJson(u));
	}
	@RequestMapping("/delete")
	public void delete(@RequestBody Map<String,String> map,HttpServletResponse response) 
	{
		int result=userService.delete(map.get("id"));
		Util u=new Util();
		if(result==1) 
		{
			u.setStatus(HttpStatus.OK);
			u.setMsg("删除成功!");
		}else 
		{
			u.setStatus(HttpStatus.ERROR);
			u.setMsg("删除失败！");
		}
		HttpUtil.send(response, JsonUtils.ToJson(u));
	}
	@RequestMapping("/findById")
	public void findById(HttpServletRequest req,HttpServletResponse response) 
	{
		String id=req.getParameter("id");
		User user=userService.findById(id);
		Util u=new Util();
		if(user==null) 
		{
			u.setStatus(HttpStatus.OK);
			u.setMsg("没有该用户!");
		}else 
		{
			u.setUrl(JsonUtils.ToJson(user));
			u.setStatus(HttpStatus.ERROR);
			u.setMsg("存在用户！");
		}
		HttpUtil.send(response, JsonUtils.ToJson(u));
	}
	
	@RequestMapping("/checkMsg")
	public void checkMsg(@RequestBody Map<String, String> map,HttpServletRequest req,HttpServletResponse resp) 
	{
		User user=userService.findById(map.get("id"));
		Util u=new Util();
		HttpSession session=req.getSession();
		String uuid="";
		if(user.getName().equals(map.get("name"))&&user.getPhoneNumber().equals(map.get("phoneNumber"))&&
				user.getIdCard().equals(map.get("idCard"))) 
		{
			u.setMsg("身份通过验证！");
			u.setStatus(HttpStatus.OK);
			uuid=UUID.randomUUID().toString().replaceAll("-", "");
			u.setUrl(uuid);
			session.setAttribute(uuid, user);
		}else {
			u.setMsg("身份未通过验证！");
			u.setStatus(HttpStatus.ERROR);
		}
		HttpUtil.send(resp, JsonUtils.ToJson(u));
	}
	
	@RequestMapping("/changePWByFind")
	public void changePWByFind(@RequestBody Map<String, String> map,HttpServletRequest request,HttpServletResponse response) 
	{
		String uuid=map.get("uuid");
		String password=map.get("password");
		HttpSession session=request.getSession();
		Util u=new Util();
		User user;
		if(null!=uuid&&!"".equals(uuid)) 
		{
			user=(User) session.getAttribute(uuid);
			if(null!=user) 
			{
				user.setPassword(password);
				try {
					userService.update(user);
					session.removeAttribute(uuid);
					u.setMsg("密码更新成功！！");
					u.setStatus(HttpStatus.OK);
					HttpUtil.send(response, JsonUtils.ToJson(u));
				} catch (Exception e) {
					u.setMsg("密码更新失败！！");
					u.setStatus(HttpStatus.ERROR);
					HttpUtil.send(response, JsonUtils.ToJson(u));
				}
			}else 
			{
				u.setMsg("身份验证失效！！");
				u.setStatus(HttpStatus.ERROR);
				HttpUtil.send(response, JsonUtils.ToJson(u));
			}
		}else 
		{
			u.setMsg("身份验证失效！！");
			u.setStatus(HttpStatus.ERROR);
			HttpUtil.send(response, JsonUtils.ToJson(u));
		}
	}
	@RequestMapping("/checkUser")
	public void checkUser(HttpServletRequest req,HttpServletResponse resp) 
	{
		String uuid=req.getParameter("uuid");
		Util u=new Util();
		if(null==uuid||"".equals(uuid)) 
		{
			u.setStatus(HttpStatus.ERROR);
			u.setMsg("身份失效，请重新登录！！");
			HttpUtil.send(resp, JsonUtils.ToJson(u));
			return;
		}
		User user=(User) req.getSession().getAttribute(uuid);
		if(null==user) 
		{
			u.setStatus(HttpStatus.ERROR);
			u.setMsg("身份失效，请重新登录！！");
			HttpUtil.send(resp, JsonUtils.ToJson(u));
		}else 
		{
			user.setPassword(null);
			u.setStatus(HttpStatus.OK);
			u.setUrl(JsonUtils.ToJson(user));
			u.setMsg("身份验证成功！！");
			HttpUtil.send(resp, JsonUtils.ToJson(u));
		}
	}
}
